package com.ad.backstage.common.utils;

import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.*;
import java.util.List;
import java.util.Random;


public class UploadFileUtils {



    private final static String UPLOADDIR = "/uploads/"; //文件上传的路径，在webContent下建立

    /**
     * 文件服务器上传路径
     */
    public static String UPLOAD_PATH = "/usr/local/zdtPic";


    /**
     * 文件服务器访问路径
     */
    public static String VISIT_PATH = "/zdtPic";


    /**
     * 小程序上传图片用
     */
    public static String upload(MultipartFile file, String dirName) {
        try {
            InputStream in = file.getInputStream();
            String dir = UPLOAD_PATH + dirName;
            File fileLocation = new File(dir);
            //此处也可以在应用根目录手动建立目标上传目录
            if (!fileLocation.exists()) {
                fileLocation.mkdir();
            }
            //上传资源
            String path = doUploadForMini(file, dir, in, dirName);

            return path;
        } catch (FileNotFoundException ex) {
            return null;
        } catch (IOException ex) {
            return null;
        }
    }


    public static String upload(MultipartFile file, HttpServletRequest request,String rootPath){
        try {
            InputStream in = file.getInputStream();
            String dir = rootPath + UPLOADDIR;
            File fileLocation = new File(dir);

            //此处也可以在应用根目录手动建立目标上传目录
            if(!fileLocation.exists()){
                fileLocation.mkdir();
            }

            //上传资源
            String path = doUpload(file, dir, in);

            return path;
        } catch (FileNotFoundException ex) {
            return null;
        } catch (IOException ex) {
            return null;
        }
    }

    /**
     * 上传资源
     *
     * @param file 资源文件
     * @param dir  存储路径
     * @param dir  资源文件输入流
     * @return
     */
    private static String doUploadForMini(MultipartFile file, String dir, InputStream in, String visitDir) throws IOException {
        String[] names = file.getOriginalFilename().split("\\.");
        String fileName = getRandomString(20) + "." + names[names.length - 1];

        File uploadFile = new File(dir, fileName);
        OutputStream out = new FileOutputStream(uploadFile);
        byte[] buffer = new byte[1024 * 1024];
        int length;
        while ((length = in.read(buffer)) > 0) {
            out.write(buffer, 0, length);
        }
        in.close();
        out.close();

        return VISIT_PATH + visitDir + File.separator + fileName;
    }


    /**
     * 上传资源
     * @param  file 资源文件
     * @param  dir  存储路径
     * @param  dir  资源文件输入流
     * @return
     */
    private static String doUpload(MultipartFile file,String dir,InputStream in) throws IOException {
        String[] names = file.getOriginalFilename().split("\\.");
        String fileName=getRandomString(20) + "." + names[names.length-1];

        File uploadFile = new File(dir, fileName);
        OutputStream out = new FileOutputStream(uploadFile);
        byte[] buffer = new byte[1024 * 1024];
        int length;
        while ((length = in.read(buffer)) > 0) {
            out.write(buffer, 0, length);
        }
        in.close();
        out.close();

        return UPLOADDIR + fileName;
    }

    /**
     * 资源名称生成规则
     * @param length
     * @return
     */
    public static String getRandomString(int length){
        String str="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
        Random random=new Random();
        StringBuffer sb=new StringBuffer();
        for(int i=0;i<length;i++){
            int number=random.nextInt(62);
            sb.append(str.charAt(number));
        }
        return sb.toString();
    }

    public static void delete(String path,HttpServletRequest request) {
        String dir = request.getServletContext().getRealPath("") + path;
        File fileLocation = new File(dir);
        fileLocation.delete();
    }

    public static void deleteBatch(List<String> paths, HttpServletRequest request) {
        String dir;
        for(String p : paths){
            dir = request.getServletContext().getRealPath("") + p;
            File fileLocation = new File(dir);
            fileLocation.delete();
        }
    }



}
